package com.lanxin.oa.personnel.service;

import com.lanxin.bean.*;
import com.lanxin.oa.personnel.dao.EmpDao;
import com.lanxin.util.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class EmpServiceImpl implements EmpService {

    @Autowired
    private EmpDao empDao;

    @Override
    public PageBean<ZzEmployee> getEmpPage(Integer page, Integer pageSize, ZzEmployee emp) {
        return empDao.selectEmpPage(page, pageSize, emp);
    }

    @Override
    public List<ZzDepartment> listDeptName() {

        return empDao.selectDeptName();
    }

    @Override
    public List<JcBasicdatavalue> selectJobs() {
        return empDao.selectBasic();
    }

    @Override
    public List<JcBasicdatavalue> selectEducations() {
        return empDao.selectBasicEducation();
    }

    @Override
    public ZzEmployee empById(Integer oid) {
        return empDao.selectEmpById(oid);
    }

    @Override
    public void add(Object o) {
        empDao.add(o);
    }


    @Override
    public void update(Object o) {
        empDao.update(o);
    }

    @Override
    public boolean selectByUserNameService(String UserName, Integer aid) {
        ZzAccount account = empDao.selectByUserName(UserName, aid);
        if (account != null) {
            return true;
        }
        return false;
    }

    @Override
    public void editemp(ZzEmployee emp, ZzAccount account, ZzJobinfo jobInfo, Date[] startDate, Date[] endDate, String[] companyName, String[] deptName, String[] jobName, String[] proofer, String[] phone, String[] description) {
        account.setInactive("q");
        account.setEmployee(emp);
        emp.setAccount(account);//添加账号
        jobInfo.setEmployee(emp);
        emp.setJobinfo(jobInfo);//添加工作信息
        emp.setState("Y");
        if (proofer != null && !"".equals(proofer)) {
            for (int i = 0; i < proofer.length; i++) {
                ZzExprience z = new ZzExprience();
                z.setStartDate(startDate[i]);
                z.setEndDate(endDate[i]);
                try {
                    z.setCompanyName(companyName[i]);
                } catch (Exception e) {
                    z.setCompanyName(null);
                }
                try {
                    z.setDeptName(deptName[i]);
                } catch (Exception e) {
                    z.setDeptName(null);
                }
                try {
                    z.setJobName(jobName[i]);
                } catch (Exception e) {
                    z.setJobName(null);
                }
                try {
                    z.setProofer(proofer[i]);
                } catch (Exception e) {
                    z.setProofer(null);
                }
                try {
                    z.setPhone(phone[i]);
                } catch (Exception e) {
                    z.setPhone(null);
                }
                z.setDescription(description[i]);
                emp.getExpriences().add(z);
            }
        }
        empDao.saveOrUpdate(emp);

    }

    @Override
    public boolean deleteEmp(Integer oid) {
        List<ZzDepartment> listdept = empDao.selectDept(oid);
        if (listdept != null && listdept.size() > 0) {
            return false;
        }
        List<LcApproval> listapp = empDao.selectAppro(oid);
        if (listapp != null && listapp.size() > 0) {
            return false;
        }
        ZzEmployee employee = new ZzEmployee();
        employee.setOid(oid);
        ZzAccount account = new ZzAccount();
        account.setAid(employee.getOid());
        account.setEmployee(employee);
        employee.setAccount(account);
        empDao.delete(employee);
        return true;
    }

    @Override
    public boolean updateEmpStatue(Integer oid, String state) {
        List<LcApproval> listapp = empDao.selectAppro(oid);
        if (listapp != null && listapp.size() > 0) {
            return false;
        }
        ZzEmployee employee = (ZzEmployee) empDao.getById(ZzEmployee.class, oid);
        employee.setState(state);
        empDao.update(employee);
        return true;
    }


}
